<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//货币管理
class Currency extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/CurrencyModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$filter = array();
		$arr = array(
			'filter'=>$filter
		);
		
		$result = parent::_get_list($arr);
		if(!$result){
			$this->err_out($this->getError());
		}

		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取全部
	* @return 成功返回1
	*/
	//end
	function get_all(){
		
		$list = $this->CurModel->get_all();
		
		foreach($list as $key=>$val){
			$list[$key] = $this->info_out($val);
		}
		
		$this->data_out($list);
	}

	function return_all(){
		
		$list = $this->CurModel->get_all();
		
		foreach($list as $key=>$val){
			$list[$key] = $this->info_out($val);
		}
		
		return $list;
	}
	
	function info_out($info){
		$info['addtime'] = date('Y-m-d H:i:s',$info['addtime']);
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		
		if(!$this->check_priv('currency_update|currency_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array(
			'name'=>'请输入名称',
			'exchange_rate'=>'请输入汇率'
		);
		$_POST['addtime'] = time();
		$post = array('name','exchange_rate','mark','addtime');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);

		$result = parent::_save($farr);
		if(!$result){
			$this->err_out($this->getError());
		}

		//写入缓存
		$rel = $this->return_all();
		parent::cache_write('currency_cache',$rel);
		
		$this->add_log("保存币种【{$result['id']}】信息",'bumen');
		$this->data_out($result);
	}
	
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('currency_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		//写入缓存
		$rel = $this->return_all();
		parent::cache_write('currency_cache',$rel);
		
		$this->add_log("删除币种【{$result}】",'bumen');
		$this->data_out($result);
		
	}
	
	//start
	/**
	* @name 批量删除
	* @param ids ture int_array ID组合
	* @return 成功返回1
	*/
	//end
	function batch_del(){
		$ids = $this->input->post('ids',null,array());

		
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		$this->CurModel->del("id in(".implode(',',$ls_ids).")");

		//写入缓存
		$rel = $this->return_all();
		parent::cache_write('currency_cache',$rel);
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 获取币种历史
	* @param name ture char 币种名称
	* @param page false int 页数
	* @param size false int 页面数据量
	* @return 成功返回1
	*/
	//end
	function get_crrency_history(){
		$name = $this->input->post('name',null,'');
		$page = $this->input->post('page',null,1);
		$size = $this->input->post('size',null,10);
		
		$arr = array(
			'name'=>$name
		);
		$res = $this->CurModel->get_list($arr,$page,$size);
		$list = $res['list'];
		
		foreach($list as $key=>$val){
			$list[$key]['addtime'] = date('Y-m-d H:i:s',$val['addtime']);
		}
		$res['list'] = $list;
		$this->data_out($res);
	}
	
	//start
	/**
	* @name 获取最新的货币
	* @return 成功返回1
	*/
	//end
	function get_new(){
		
		$res = $this->CurModel->getRow(array(),'*','addtime','desc');

		$info = array(
			'date'=>$res?date('Y-m-d',$res['addtime']):date('Y-m-d')
		);
		
		
		$res = $this->CurModel->date_get_list($info['date']);
		
		
		$info['list'] = $res?$res:array();
		
		
		$this->data_out($info);
	}

	//start
	/**
	* @name 只获取固定的美元和港币
	* @return 成功返回1
	*/
	//end
	function get_fixed_coin(){
		$res = $this->CurModel->get_fixed_coin();
		
		
		$info['list'] = $res?$res:array();
		
		
		$this->data_out($info);
	}

	//start
	/**
	* @name 获取列表  同种名称只能出现最新的一条
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_newList(){
		
		$result = $this->CurModel->get_all();
		if(!$result){
			$this->err_out($this->getError());
		}

		$sort = array_column($result, 'addtime');
		array_multisort($sort, SORT_DESC, $result);

		foreach($result as $key=>$val){
			$result[$key] = $this->info_out($val);
		}
		
		$this->data_out($result);
	}
}
